package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.CustomerTraceHistory;
import cn.ytr.crm.domain.Employee;
import cn.ytr.crm.mapper.CustomerTraceHistoryMapper;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ICustomerTraceHistoryService;
import cn.ytr.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CustomerTraceHistoryServiceImpl implements ICustomerTraceHistoryService{

	@Autowired
	private CustomerTraceHistoryMapper customerTraceHistoryMapper;

	/**
	 * 增加或保存客户跟踪记录
	 */
	@Override
	public void saveOrUpate(CustomerTraceHistory entity) {
		if (entity.getId() != null) {
			customerTraceHistoryMapper.updateByPrimaryKey(entity);
		} else {
			//录入员工为当前登录员工
			Employee employee = UserContext.getCurrentUserByShiro();
			entity.setInputUser(employee);
			//录入时间为当前时间
			entity.setInputTime(new Date());
			customerTraceHistoryMapper.insert(entity);
		}
	}

	/**
	 * 删除客户跟踪记录
	 */
	@Override
	public void delete(Long id) {
		customerTraceHistoryMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取单个客户跟踪记录
	 */
	@Override
	@Transactional(readOnly=true)
	public CustomerTraceHistory get(Long id) {
		return customerTraceHistoryMapper.selectByPrimaryKey(id);
	}

	/**
	 * 获取所有客户跟踪记录
	 */
	@Override
	@Transactional(readOnly=true)
	public List<CustomerTraceHistory> listAll() {
		return customerTraceHistoryMapper.selectAll();
	}

	/**
	 * 高级查询所有客户跟踪记录
	 */
	@Override
	@Transactional(readOnly=true)
	public PageInfo query(QueryObject qo) {
		/*Integer count = CustomerTraceHistoryMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<CustomerTraceHistory> list = CustomerTraceHistoryMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}*/

		//改用分页插件 pageHelper
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<CustomerTraceHistory> list = customerTraceHistoryMapper.selectForList(qo);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}

	

}








































